package com.longtop.framework.sys.file.supports;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.longtop.framework.security.BaseUncheckedException;
import com.longtop.framework.util.DateUtil;
import com.longtop.framework.util.StringUtil;

/**
 * 
 * 上传文件工具类，继承UpLoadBase，UpLoadBase在fileBean-service.xml已经初始化Bean
 * 
 */
public class UpLoadUtils extends UpLoadBase {
	private String beanConfig = "";
	String uploadPath = "";

	private Map<String, Object> contentType;

	@SuppressWarnings("unchecked")
	public UpLoadedFiles uploadFile(HttpServletRequest request) throws BaseUncheckedException {
		UpLoadedFiles upLoad = new UpLoadedFiles();
		try {
			myUpload = new BaseMultipartRequest();
			// 允许文件
			myUpload.setAllowFile(allowedFiles);
			// 不允许文件
			myUpload.setNotAllowFile(deniedFiles);
			// 命名策略
			myUpload.setPolicy(baseNamePolicy);

			savaBasePath = StringUtil.replace(savaBasePath, "\"", "/");
			savaBasePath = StringUtil.replace(savaBasePath, "//", "/");
			if (!StringUtil.isEndWith(savaBasePath, "/"))
				savaBasePath += "/";
			String savePath = savaBasePath;
			if (datePackage) {
				savePath += DateUtil.getDatePath(DateUtil.now());
				savePath = StringUtil.replace(savePath, "\"", "/");
				savePath = StringUtil.replace(savePath, "//", "/");
				if (!StringUtil.isEndWith(savePath, "/"))
					savePath += "/";
			}
			if (contextPath) {
				uploadPath = request.getSession().getServletContext().getRealPath(savePath);
			} else {
				String rootPosiztion = StringUtil.getString(root, "");
				// if(rootPosiztion.length()>1){
				// rootPosiztion = StringUtil.replace(rootPosiztion, "\"", "/");
				// rootPosiztion = StringUtil.replace(rootPosiztion, "//", "/");
				// }
				if (StringUtil.contains(system, "windows")) {
					if (rootPosiztion.equals("")) {
						rootPosiztion = "C";
					}
					rootPosiztion = rootPosiztion.substring(0, 1);
					rootPosiztion = rootPosiztion + ":/";
				} else {
					if (!StringUtil.isEndWith(rootPosiztion, "/")) {
						rootPosiztion += "/";
					}
				}
				uploadPath = rootPosiztion + savePath;
			}
			File temUploadPath = new File(uploadPath);
			if (!temUploadPath.exists()) {
				temUploadPath.mkdirs();
			}
			myUpload.MultipartUpLoad(request, uploadPath, maxFileSize * 1024, enCode);
			upLoad.setMyUpload(myUpload);
			// upLoad.setParameterMap(myUpload.getParameterMap());
			// upLoad.setParams(myUpload.getParameters());
			// upLoad.setParamsnames(myUpload.getParameterNames());
			//			
			// upLoad.setFilename(myUpload.getFileNames());
			List<UploadFile> fileList = new ArrayList<UploadFile>();
			Map<String, Object> uploadFileMap = new HashMap<String, Object>();
			Enumeration files = myUpload.getFileNames();

			while (files.hasMoreElements()) {
				String name = (String) files.nextElement();// "filename"
				if (myUpload.isArray(name)) {// 判断文件是否有多个
					UploadedFile[] upeds = myUpload.getUploadedFile(name);
					UploadFile[] ups = new UploadFile[upeds.length];
					for (int i = 0; i < ups.length; i++) {
						String filename = upeds[i].getFilesystemName();
						String originalFilename = upeds[i].getOriginalFileName();
						String type = upeds[i].getContentType();
						File f = upeds[i].getFile();
						long size = upeds[i].getSize();
						UploadFile up = new UploadFile();
						up.setBeanConfig(beanConfig);
						up.setRequestName(name);
						up.setRelativePath(savePath);
						up.setFileContentType(type);
						up.setFileName(originalFilename);
						up.setContextPath(contextPath);
						up.setFileSize(StringUtil.getFileSize(size));
						up.setSaveFileName(filename);
						up.setSavePath(savePath + filename);
						up.setFileExt(StringUtil.getFileEXT(filename));
						up.setFileLength(size);
						up.setFileObject(f);
						ups[i] = up;
						fileList.add(up);
					}
					uploadFileMap.put(name, ups);
				} else {

					String filename = myUpload.getFilesystemName(name);
					String originalFilename = myUpload.getOriginalFileName(name);
					String type = myUpload.getContentType(name);
					File f = myUpload.getFile(name);
					long size = myUpload.getFileSize(name);
					UploadFile up = new UploadFile();
					up.setRequestName(name);
					up.setRelativePath(savePath);
					up.setFileContentType(type);
					up.setFileName(originalFilename);
					up.setBeanConfig(beanConfig);
					up.setFileSize(StringUtil.getFileSize(size));
					up.setSaveFileName(filename);
					up.setSavePath(savePath + filename);
					up.setContextPath(contextPath);
					up.setFileExt(StringUtil.getFileEXT(filename));
					up.setFileLength(size);
					up.setFileObject(f);
					uploadFileMap.put(name, up);
					fileList.add(up);
				}
			}
			upLoad.setUploadFileMap(uploadFileMap);
			upLoad.setUpLoadFiles(fileList);
		} catch (IOException lEx) {
			lEx.printStackTrace();
			throw new BaseUncheckedException(lEx.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new BaseUncheckedException(e.getMessage());
		}

		// 解析EXCEL模板重要方法！
		if (excelAnalyser != null) {
			excelAnalyser.parseUpLoadFiles(upLoad);
		}

		return upLoad;
	}

	public String getBeanConfig() {
		return beanConfig;
	}

	public void setBeanConfig(String beanConfig) {
		this.beanConfig = beanConfig;
	}

	public Map<String, Object> getContentType() {
		return contentType;
	}

	public void setContentType(Map<String, Object> contentType) {
		this.contentType = contentType;
	}

}
